package net.tycmc.cty.ctyms.timejob.common;


/**
 * 4.7数据质量
 * 统一计算类
 * 工程中涉及到计算的方法的集合，异常数据处理，均在该方法中
 * author：wangkai
 * time：2015-06-27
*/
public class UnifideCaculation {

	/**
	 *百公里平均油耗（百公里行驶油耗）（L/100Km）
	 *param Km:里程值,L:油耗值,Scale：精确小数点的位数 
	 *return String 
	*/
	public static String AverageLKm(Object Km,Object L,int Scale){
		double runKm = 0f;
		double runL = 0f;
		String averageLKm = "";
		//异常值处理,数据为空
		if(Km == null || L == null || Km.toString().equals("") || L.toString().equals(""))
			return "--";
		//异常值处理,数据为"--"
		if(Km.toString().equals("--")||L.toString().equals("--"))
			return "--";
		runKm =  Double.parseDouble(Km.toString());
		runL =  Double.parseDouble(L.toString());
		if(runKm==0 && runL == 0)
			return "0";
		//异常值处理
		if(runKm<0 || runL<0 || (runKm==0 && runL !=0) || (runKm !=0 && runL==0))
			return "--";
		//计算百公里平均油耗：油耗/里程*100
		double avLKm = NumberFormat.round(NumberFormat.div(runL*100,runKm,Scale), Scale);
		if(avLKm>=0 && avLKm<=100){
			averageLKm = avLKm+"";
			//进行补0，如保留两位小数结果为2.5需返回2.50
			averageLKm = UnifideCaculation.add0(averageLKm, Scale);
		}else{
			averageLKm = "--";
		}
		return averageLKm;
	}
	
	/**
	 *平均速度（Km/H）
	 *param Km:里程值,H:工作小时数,Scale：精确小数点的位数 
	 *return String 平均速度（Km/H）
	*/
	public static String AverageSpeed(Object Km,Object H,int Scale){
		double runKm = 0f;
		double runH = 0f;
		String averageSpeed = "";
		if(Km == null || H == null || Km.toString().equals("") || H.toString().equals(""))
			return "--";
		if(Km.toString().equals("--")||H.toString().equals("--"))
			return "--";
		runKm =  Double.parseDouble(Km.toString());
		runH =  Double.parseDouble(H.toString());
		if(runKm==0 && runH == 0)
			return "0";
		//异常值处理
		if(runKm<0 || runH<0 || (runKm==0 && runH !=0) || (runKm !=0 && runH==0))
			return "--";
		//计算平均时速 里程/时间
		double avSpeed = NumberFormat.round(NumberFormat.div(runKm,runH,Scale), Scale);
		if(avSpeed>=0 && avSpeed<=250.996){
			averageSpeed = avSpeed+"";
			//进行补0，如保留两位小数结果为2.5需返回2.50
			averageSpeed = UnifideCaculation.add0(averageSpeed, Scale);
		}else{
			averageSpeed = "--";
		}
		return averageSpeed;
	}
	
	/**
	 *小时油耗（L/H）
	 *param L:油耗值,H:工作小时数,Scale：精确小数点的位数 
	 *return String 小时油耗（L/H）
	*/
	public static String HourOils(Object L,Object H,int Scale){
		double runL = 0f;
		double runH = 0f;
		String hourOils = "";
		if(L == null || H == null || L.toString().equals("") || H.toString().equals(""))
			return "--";
		if(L.toString().equals("--")||H.toString().equals("--"))
			return "--";
		runL =  Double.parseDouble(L.toString());
		runH =  Double.parseDouble(H.toString());
		if(runL==0 && runH == 0)
			return "0";
		//异常值处理
		if(runL<0 || runH<0 || (runL==0 && runH !=0) || (runL !=0 && runH==0))
			return "--";
		//计算小时油耗：油耗/时间
		double hOils = NumberFormat.round(NumberFormat.div(runL,runH,Scale), Scale);
		if(hOils>=0){
			hourOils = hOils+"";
			//进行补0，如保留两位小数结果为2.5需返回2.50
			hourOils = UnifideCaculation.add0(hourOils, Scale);
		}else{
			hourOils = "--";
		}
		return hourOils;
	}

	/**
	 *尿素消耗量和燃油消耗的比值
	 *param UL:尿素消耗量值,OL:油消值,Scale：精确小数点的位数 
	 *return String 尿素消耗量和燃油消耗的比值
	*/
	public static String UreaOils(Object UL,Object OL,int Scale){
		double runUL = 0f;
		double runOL = 0f;
		String ureaOils = "";
		if(UL == null || OL == null || UL.toString().equals("") || OL.toString().equals(""))
			return "--";
		if(UL.toString().equals("--")||OL.toString().equals("--"))
			return "--";
		runUL =  Double.parseDouble(UL.toString());
		runOL =  Double.parseDouble(OL.toString());
		if(runUL==0 && runOL == 0)
			return "0";
		//异常值处理
		if(runUL<0 || runOL<0 || (runUL !=0 && runOL==0))
			return "--";
		//计算小时油耗：油耗/时间
		double uOils = NumberFormat.round(NumberFormat.div(runUL,runOL,Scale), Scale);
		if(uOils>=0){
			ureaOils = uOils+"";
			//进行补0，如保留两位小数结果为2.5需返回2.50
			ureaOils = UnifideCaculation.add0(ureaOils, Scale);
		}else{
			ureaOils = "--";
		}
		return ureaOils;
	}
	
	/**
	 *百公里尿素消耗量
	 *param Km:里程值,L:尿素消耗量值,Scale：精确小数点的位数 
	 *return String 百公里尿素消耗量
	*/
	public static String  AverageUreaLKm(Object Km,Object L,int Scale){
		double runKm = 0f;
		double runUreaL = 0f;
		String AvUreaOils = "";
		if(Km == null || L == null || Km.toString().equals("") || L.toString().equals(""))
			return "--";
		if(Km.toString().equals("--")||L.toString().equals("--"))
			return "--";
		runKm =  Double.parseDouble(Km.toString());
		runUreaL =  Double.parseDouble(L.toString());
		if(runKm==0 && runUreaL == 0)
			return "0";
		//异常值处理
		if(runKm<0 || runUreaL<0 || (runKm==0 && runUreaL !=0) || (runKm !=0 && runUreaL==0))
			return "--";
		//计算小时油耗：油耗/时间
		double auOils = NumberFormat.round(NumberFormat.div(runUreaL*100,runKm,Scale), Scale);
		if(auOils>=0){
			AvUreaOils = auOils+"";
			//进行补0，如保留两位小数结果为2.5需返回2.50
			AvUreaOils = UnifideCaculation.add0(AvUreaOils, Scale);
		}else{
			AvUreaOils = "--";
		}
		return AvUreaOils;
	}
	
	/**
	 * 千公里次数计算方法,急刹车，制动刹车，全油门，急加速等千公里次数
	 * @param Km:里程,Count:次数,Scale:精确小数点的位数
	 * 
	 * @return String result 结果,统一处理数据类按照传入的小数点位数四舍五入后返回
	 * @author wangkai
	 * 
	 * */
	public static String KilometersPercent(Object Km,Object Count,int Scale){
		
		String result = "0";
		double runKm = 0f;
		double count = 0f;
		
		if(Km == null || Count ==null || Km.toString().equals("") || Count.toString().equals("") || Km.toString().equals("--") || Count.toString().equals("--"))
			return result;
		runKm = Double.parseDouble(Km.toString());
		count = Double.parseDouble(Count.toString());
		if(runKm<0 || count<0)
			return "--";
		result = NumberFormat.round(NumberFormat.div(count*1000,runKm,Scale), Scale)+"";
		return result;
	}
	
	/**百分比计算
	 * @param totalTime:除数,otherTime:被除数（怠速时长、超长怠速时长等）,Scale:精确小数点的位数
	 * 载重利用率按时间加权也用到此方法，
	 * @return String result 结果,若【其他数据】值不为空，则返回【小数转换成百分数】（需进行四舍五入）；否则，则返回“--”
	 * @author wangkai
	 * 
	 * */
	public static String TimePercentProcess(Object totalTime,Object otherTime,int Scale){
		String result = "";
		double totaltime = 0f;
		double othertime = 0f;
		if(totalTime==null || totalTime.toString().equals("") || totalTime.toString().equals("--") || otherTime==null || otherTime.toString().equals("") || otherTime.toString().equals("--")){
			return "--";
		}
		totaltime = Double.parseDouble(totalTime.toString());
		othertime = Double.parseDouble(otherTime.toString());
		if(totaltime<=0)
			return "--";
		//计算百分比时将计算出来的小数保留位数为Scale+4，如0.5888478换算成百分比为
		result = ExceptionDataProcess.ComParePrecent(NumberFormat.div(othertime,totaltime,Scale+4),Scale);
		return result;
		
	}
	
	/**
	 * 平均每天工作时长
	 * @param Hours:工作小时数,BeginTime:开始时间，EndTime:结束时间,Scale:精确小数点的位数
	 * 
	 * @return String hours 工作小时数,若【工作小时数】值在【0，24*天数】范围内，则返回【工作小时数】（需进行四舍五入）；若不在范围内，则返回“--”
	 * @author wangkai
	 * */
	public static String HoursPreDay(Object Hours,String BeginTime,String EndTime,int Scale){
		
		if(Hours==null || Hours.toString().equals("") || Hours.toString().equals("--")){
			return "--";
		}
		if(BeginTime==null || BeginTime.equals("") || BeginTime.equals("--")){
			return "--";
		}
		if(EndTime==null || EndTime.equals("") || EndTime.equals("--")){
			return "--";
		}
		String hoursPreDay =  Hours.toString();
		String bgnTime = BeginTime;
		String endTime = EndTime;
		int days = 0;
		//处理开始时间和结束时间，让时间为：yyyy-MM-dd HH:mm:ss
		if(bgnTime.length()<11){
			bgnTime += " 00:00:00";
		}else if(bgnTime.length()<14){
			bgnTime += ":00:00";
		}else if(bgnTime.length()<17){
			bgnTime += ":00";
		}
		if(endTime.length()<11){
			endTime += " 23:59:59";
		}else if(endTime.length()<14){
			endTime += ":59:59";
		}else if(endTime.length()<17){
			endTime += ":59";
		}
		if(!DateRegUtil.compare(endTime,bgnTime,"yyyy-MM-dd HH:mm:ss")){
			System.out.println("方法:Hours 执行:结束日期大于开始日期异常，开始日期："+BeginTime+"，结束日期："+EndTime);
			return "--";
		}else{
			//获取开始时间和结束时间的间隔天数
			days = DateRegUtil.getDaysOfTimeDef(new String[]{bgnTime,endTime});
			//若【工作小时数】值在【0，24*天数】范围内，则返回【工作小时数】（需进行四舍五入）；若不在范围内，则返回“--”
			hoursPreDay = NumberFormat.round(NumberFormat.div(Double.parseDouble(hoursPreDay),days,Scale),Scale)+"";
		     }
		return hoursPreDay;
	}
	
	/**
	 * 总耗
	 * @param runKm:里程(km),Load:载重（吨）,Scale:精确小数点的位数
	 * 
	 * @return String totalConsume 总耗,计算方法：里程*载重
	 * @author wangkai
	 * */
	public static String TotalConsume(Object RunKm,Object Load,int Scale){
		
		if(RunKm==null || RunKm.toString().equals("") || RunKm.toString().equals("--")){
			return "--";
		}
		if(Load==null || Load.equals("") || Load.equals("--")){
			return "--";
		}
		double runKm = Double.parseDouble(RunKm.toString());
		double load = Double.parseDouble(Load.toString());
		String totalConsume = NumberFormat.round(NumberFormat.mul(runKm,load),Scale)+"";
		return totalConsume;
	}
	
	/**
	 * 单耗
	 * @param TotalConsume:每段总耗的和(TKm),L:每段油耗的和(L),Scale:精确小数点的位数
	 * 
	 * @return String singleConsume 单耗,计算方法：油耗/里程*载重
	 * @author wangkai
	 * */
	public static String SingleConsume(Object TotalConsumeSum,Object RunL,int Scale){
		
		if(TotalConsumeSum==null || TotalConsumeSum.toString().equals("") || TotalConsumeSum.toString().equals("--")){
			return "--";
		}
		if(RunL==null || RunL.equals("") || RunL.equals("--")){
			return "--";
		}
		double totalConsumeSum = Double.parseDouble(TotalConsumeSum.toString());
		double runL = Double.parseDouble(RunL.toString());
		
		String singleConsume = NumberFormat.round(NumberFormat.div(runL,totalConsumeSum,Scale),Scale)+"";
		return singleConsume;
	}
	
	/**
	 * 小时转换时分秒
	 * @param Hours:小时
	 * 
	 * @return String result xx小时xx分xx秒
	 * @author wangkai
	 * */
	public static String ChangeTime(Object Hours){
		
		if(Hours==null || Hours.toString().equals("") || Hours.toString().equals("--")||Double.parseDouble(Hours.toString())<0){
			return "--";
		}
		double hours = Double.parseDouble(Hours.toString());
		double totalSec = hours*3600;
		String hour = "";
		if(Hours.toString().lastIndexOf(".")>0){
			hour = Hours.toString().substring(0,Hours.toString().lastIndexOf("."));
		}else{
			hour = Hours.toString();
		}
		 
		String minute = (int)totalSec%3600/60+"";
		String second = (int)totalSec%3600%60+"";
		
		return hour+"小时"+minute+"分钟"+second+"秒" ;
	}
	
	/**
	 * 补0方法，如计算结果需要保留2位小数，结果为2.5需返回2.50
	 * param result：传入的结果值，num：保留的小数位数
	 * return String 补0后的结果
	 * */
	public static String add0(String result,int num){
		int dlength = result.substring(result.indexOf(".")+1, result.length()).length();
		if(dlength<num){
			for(int i=0;i<num-dlength;i++){
				result = result+"0";
			}
		}
		return result;
	}
	public static void main(String[] args) {
		String av = UnifideCaculation.AverageLKm("4", "1", 5);
		System.out.println(av);
		//System.out.println(NumberFormat.round(3/7,7));
		String avsp = UnifideCaculation.AverageSpeed("7", "3", 3);
		System.out.println(avsp);
		System.out.println(NumberFormat.div(4,1));
		String averageLKm = "2.333";
		int aa = averageLKm.substring(averageLKm.indexOf(".")+1, averageLKm.length()).length();
		System.out.println(aa);
		String ho = UnifideCaculation.HourOils("4", "1", 5);
		System.out.println(ho);
		String uo = UnifideCaculation.UreaOils("4", "1", 5);
		System.out.println(uo);
		String aul = UnifideCaculation.AverageUreaLKm("4", "1", 5);
		System.out.println(aul);
		String time  = UnifideCaculation.TimePercentProcess(3,10,2);
		System.out.println(time);
		String hours = "6.2385";
		String hour = hours.substring(0,hours.lastIndexOf("."));
		String minute = hours.substring(hours.lastIndexOf("."),hours.length());
		System.out.println(hour);
		System.out.println(minute);
		String changetime = UnifideCaculation.ChangeTime("-1");
		System.out.println(changetime);

	}
}
